package com.c2c.service.impl;

import com.c2c.mapper.GoodsClassifyMapper;
import com.c2c.mapper.GoodsMapper;
import com.c2c.po.Goods;
import com.c2c.po.GoodsClassify;
import com.c2c.po.enums.impl.GoodiesState;
import com.c2c.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    GoodsClassifyMapper goodsClassifyMapper;

    @Override
    public List<Goods> findAllGoods() {
        return goodsMapper.findAllGoods();
    }

    @Override
    public Goods findById(int goodId) {
        return goodsMapper.findById(goodId);
    }

    @Override
    public List<Goods> findByUserId(int userAccount) {
        return goodsMapper.findByUserId(userAccount);
    }

    @Override
    public List<Goods> findByClassifyId(int goodsClassifyId) {
        return goodsMapper.findByClassifyId(goodsClassifyId);
    }

    @Override
    public List<Goods> findBySomething(String string, int goodsClassifyId) {
        return goodsMapper.findBySomething(string,goodsClassifyId);
    }

    @Override
    public void updateAllById(Goods goods, int goodsState) {
        for (GoodiesState goodiesState: GoodiesState.values()){
            if (goodiesState.getCode() == goodsState){
                goods.setGoodsState(goodiesState);
                goodsMapper.updateAllById(goods);
            }
        }
    }

    @Override
    public void updateStateById(Goods goods ,int goodsState) {
        for (GoodiesState goodiesState: GoodiesState.values()){
            if (goodiesState.getCode() == goodsState){
                goods.setGoodsState(goodiesState);
                goodsMapper.updateStateById(goods);
            }
        }
    }

    @Override
    public void insertGoods(Goods goods) {
        //封入当前时间
        goods.setGoodsShelfTime(new Date());
        goodsMapper.insertGoods(goods);
    }

    @Override
    public List<GoodsClassify> findAll() {
        return goodsClassifyMapper.findAll();
    }

    @Override
    public void insertClassify(GoodsClassify goodsClassify) {
        goodsClassifyMapper.insertClassify(goodsClassify);
    }
}
